/**
 * 地图管理器
 */
cc.Class({
    extends: cc.Component,

    properties: {
        /**
         * 生成的圆圈总数
         */
        number: {
            default: 0,
            tooltip: "生成的圈的总个数"
        },
        /**
         * 每个圆圈的间隔
         */
        gap: {
            default: 0,
            tooltip: "每个圈的间隔"
        },
        movingSpeed: {
            default: 0,
            tooltip: "圈向中心移动的速度"
        },
        /**
         * 往内缩圈的时间
         */
        scaleCircleTime: {
            default: 0,
            tooltip: "往内缩圈的时间"
        },
        /**
         * 奖励物品库
         */
        rewardPrefabs: [cc.Prefab],
        /**
         * 颜色库
         */
        circleColors: [cc.Color],
        /**
         * 圆圈的预设
         */
        circleLinePrefab: cc.Prefab,
        /**
         * 实心圆圈的预设
         */
        circleSolidPrefab: cc.Prefab,
        /**
         * 玩家节点
         */
        playerNode: cc.Node,
        /**
         * 背景节点
         */
        bgNode: cc.Node,
        /**
         * 敌人生成器节点
         */
        enemyManagerNode: cc.Node,
    },

    onLoad () {
        this._player = this.playerNode.getComponent('Player');
        this._currentColorIndex = this.number;
        this._currentRotateSpeed = -30;
        this._enemyManager = this.enemyManagerNode.getComponent('EnemyManager');
        this.createAllCircles();
        this.setMaxDifficulty();
        this.setDifficultyProb();
        this._isInner = false;
        this._isGameOver = false;
    },

    start () {
        this.bgNode.color = this.circleColors[this._currentColorIndex];
    },

    // update (dt) {},

    /**
     * 创建所有的圆圈
     */
    createAllCircles () {
        //最外圈的大小
        var currentSize = this.circleLinePrefab.data.width;
        this._maxEdge = currentSize;
        //存储所有的圈
        this._circleItem = new Array();
        //实例化所有的圈
        for (var i = 0; i < this.number; i++)
        { 
            var go = cc.instantiate(this.circleLinePrefab);
            var item = go.getComponent('CircleLineItem');
            this._circleItem[i] = item;
            item.manager = this;
            //TODO test速度
            item.rotateSpeed = 30;
            this.node.addChild(go);
            go.scaleX = go.scaleY = currentSize / this._maxEdge;
            item.changeColor(this.circleColors[this.number - i - 1]);
            //圈的半径减小
            currentSize -= this.gap;
            //最小限定
            if (currentSize < 0) {
                this._minEdge = 0;
                return;
            }
            //初始圈绑定
            if (i == 0) this.setPlayerCircle(go);
        }
        //中心放置实心圆
        var center = cc.instantiate(this.circleSolidPrefab);
        center.parent = this.node;
        center.width = center.height = currentSize;
        //缩小的最小边界范围
        this._minEdge = currentSize;

        //当前的总圈数
        this._currentCircleCount = 0;
        //当前的道具
        this._currentProp = 0;
    },

    /**
     * 获取向内移动的速度
     * @returns 速度
     */
    getSpeed () {
        if (this._isGameOver) return 0;
        return this._isInner ? this.gap / (this.scaleCircleTime * 2) : this.movingSpeed;
    },

    /**
     * 所有的圈往内移动一圈
     */
    setAllCircleInner () {
        for (var i = 0; i < this.number; i++) {
            this._circleItem[i].setInnerCircle();
        }
    },

    /**
     * 停止所有的圆圈的运动
     */
    stopAllCircle () {
        for (var i = 0; i < this.number; i++) {
            this._circleItem[i].stopAction();
        }
    },

    /**
     * 给玩家角色绑定所在圆圈
     * @param {cc.Node} circle 圆圈节点
     */
    setPlayerCircle (circle) {
        this._player.currentCircle = circle;
    },

    /**
     * 设置玩家到下一圈
     * @returns 是否可以执行到下一圈
     */
    setPlayerNextCircle () {
        var children = this.node.children;
        for (var i = 0; i < children.length; i++){
            if (this._player.currentCircle == children[i]){
                //获取到了当前层
                if (i == 0) {
                    return false;
                }
                else{
                    this.setPlayerCircle(children[i - 1]);
                    return true;
                }
            }
        }
        return false;
    },

    /**
     * 将一个圆调整到最底层
     * @param {cc.Node} circle 要改变顺序的子物体
     */
    setCircleToBottom (circle) {
        this.node.removeChild(circle);
        this.node.insertChild(circle, 0);
    },

    /**
     * 判断是否在可移动视角的圈上
     * @param {cc.Node} circle 圈的节点
     */
    isInCanJumpCircle (circle) {
        return this.node.children[0] == circle;
    },

    /**
     * 给圆盘设置顺序颜色
     * @param {CircleLineItem} item 改变的圈
     */
    setColor (item) {
        //var index = Math.floor(cc.random0To1() * this.circleColors.length);
        item.changeColor(this.circleColors[this._currentColorIndex]);
        this._currentColorIndex++;
        this._currentColorIndex %= this.circleColors.length;
        //背景颜色改为下一个颜色
        var nextColor = this.circleColors[this._currentColorIndex];
        var r = nextColor.r; var g = nextColor.g; var b = nextColor.b;
        this.bgNode.runAction(cc.tintTo(this.gap / (this.movingSpeed * 2), r, g, b));
    },

    /**
     * 给圆盘增加敌人
     * @param {CircleLineItem} circleItem 改变的圈
     * @param {number} dif 难度等级
     */
    createEnemy (circleItem, dif) {
        //TODO 管理所有的实体对象
        var enemy = this._enemyManager.getEnemyPrefab(dif);
        if (enemy != null) {
            var clone = cc.instantiate(enemy);
            //TODO 道具
            // if (clone.getComponent('Enemy') && this._currentCircleCount % 7 == 0) {
            //     clone.getComponent('Enemy').createReward(this.rewardPrefabs[this._currentProp]);
            //     this._currentProp = (this._currentProp + 1)%this.rewardPrefabs.length;
            // }
            //是否显示奖励物品
            circleItem.addEnemy(clone);
        }
    },

    /**
     * 改变圈的旋转速度
     * @param {CircleLineItem} circleItem 改变的圈
     * @param {number} speed 旋转速度
     */
    setSpeed (circleItem, speed) {
        circleItem.changeRotateSpeed(speed);
    },

    /**
     * 重置圆圈
     * @param {CircleLineItem} circleItem 圈
     */
    resetCircle (circleItem) {
        //加一圈
        this._currentCircleCount++;
        circleItem.init(this._currentCircleCount);
        this.getDifficulty(this._currentCircleCount);
        this.setColor(circleItem);
        var difficulty = this.getDifficulty(this._currentCircleCount);
        this.createEnemy(circleItem, difficulty);
        if (difficulty == 2){
            this.setSpeed(circleItem, 0);
            circleItem.jumpAround();
        }
        else {
            this.setSpeed(circleItem, this._currentRotateSpeed);
        }
        this._currentRotateSpeed = -this._currentRotateSpeed;
    },

    /**
     * 设置最大难度数组
     */
    setMaxDifficulty () {
        this._maxDifficultyRange = new Array();
        var sum = 0;
        for (var i = 0; i < this._enemyManager.typeNumber; i++) {
            sum += this._enemyManager.numberInPerDifficulty * (i + 1);
            this._maxDifficultyRange[i] = sum;
        }
    },

    /**
     * 设置最大难度下的特定难度的概率
     */
    setDifficultyProb () {
        this._difficultyProb = new Array();
        for (var i = 0; i < this._maxDifficultyRange.length; i++) {
            this._difficultyProb[i] = new Array();
            var total = 0;
            for (var j = 0; j < i + 1; j++) { total += j + 1; }
            for (var j = 0; j < i + 1; j++) {
                //前面所有难度的总和
                var preTotal = 0;
                for (var k = 1; k <= j + 1; k++) {
                    preTotal += k;
                }
                this._difficultyProb[i][j] = preTotal / total;
            }
        }
    },

    /**
     * 根据圈的层数获取一个概率难度等级
     * @param {number} circleNum 圈的层数
     * @returns 难度等级
     */
    getDifficulty (circleNum) {
        var maxDifficulty = 0;
        for (var i = 0; i < this._maxDifficultyRange.length; i++) {
            var min = i == 0 ? 0 : this._maxDifficultyRange[i - 1];
            if (circleNum > min && circleNum <= this._maxDifficultyRange[i]) {
                //得到了最大难度
                maxDifficulty = i;
                break;
            }
            //已经超越了最大难度限制范围
            if (i == this._maxDifficultyRange.length - 1) {
                maxDifficulty = i;
            }
        }
        //从最大难度中随机出难度
        var difficulty = 0;
        var randomNum = cc.random0To1();
        for (var i = 0; i < this._difficultyProb[maxDifficulty].length; i++) {
            var min = i == 0 ? 0 : this._difficultyProb[maxDifficulty][i - 1];
            if (randomNum > min && randomNum <= this._difficultyProb[maxDifficulty][i]) {
                //得到了难度
                difficulty = i;
                break;
            }
        }
        return difficulty;
    },
});
